<?php

/**
 *  2Moons
 *  Copyright (C) 2011  Slaver
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package 2Moons
 * @author Slaver <slaver7@gmail.com>
 * @copyright 2009 Lucky <lucky@xgproyect.net> (XGProyecto)
 * @copyright 2011 Slaver <slaver7@gmail.com> (Fork/2Moons)
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3 License
 * @version 1.6.1 (2011-11-19)
 * @info $Id: class.ShowTraderPage.php 2126 2012-03-11 21:11:32Z slaver7 $
 * @link http://code.google.com/p/2moons/
 */


class ShowTraderPage extends AbstractPage
{
	public static $requireModule = MODULE_TRADER;

	function __construct() 
	{
		parent::__construct();
	}
	
	public static $Charge = array(
		901	=> array(901 => 1, 902 => 2, 903 => 4, 921 => 0),
		902	=> array(901 => 0.5, 902 => 1, 903 => 2, 921 => 0),
		903	=> array(901 => 0.25, 902 => 0.5, 903 => 1, 921 => 0),
		921	=> array(901 => 0.00025, 902 => 0.0005, 903 => 0.001, 921 => 1),
	);
	
	public function show() 
	{
		global $LNG, $CONF, $USER;
		
		$this->tplObj->assign_vars(array(
			'tr_cost_dm_trader'		=> sprintf($LNG['tr_cost_dm_trader'], pretty_number($CONF['darkmatter_cost_trader']), $LNG['tech'][921]),
			'charge'				=> self::$Charge,
			'requiredDarkMatter'	=> $USER['darkmatter'] < $CONF['darkmatter_cost_trader'] ? sprintf($LNG['tr_empty_darkmatter'], $LNG['tech'][921]) : false,
		));

		$this->display("page.trader.default.tpl");
	}
		
	function trade()
	{
		global $USER, $LNG, $CONF, $reslist;
		
		if ($USER['darkmatter'] < $CONF['darkmatter_cost_trader']) {
			$this->redirectTo('game.php?page=trader');
		}
		
		$resourceID	= HTTP::_GP('resource', 0);
		
		if(!in_array($resourceID, $reslist['resstype'][4])) {
			$this->printMessage($LNG['invalid_action'],true, array("game.php?page=trader", 2) );
		}
		
		$tradeResources	= array_values(array_diff($reslist['resstype'][4], array($resourceID)));
		
		$this->tplObj->loadscript("trader.js");
		$this->tplObj->assign_vars(array(
			'resourceID'		=> $resourceID,
			'tradeRessources'	=> $tradeResources,
			'charge' 			=> self::$Charge[$resourceID],
		));

		$this->display('page.trader.trade.tpl');
	}
	
	function obmen()
	{
		global $USER, $LNG, $CONF, $reslist;
		
		$this->tplObj->loadscript("trader.js");
		$this->tplObj->assign_vars(array(
			'kursatm'			=> $CONF['resource_multiplier'] < 50 ? 0.2 : ($CONF['resource_multiplier'] < 500 ? 0.0135 : 0.00572),
			'atm'				=> $USER['atm'],
		));

		$this->display('page.trader.atm.tpl');
	}
	
	function obmenatm()
	{
		global $USER, $PLANET, $LNG, $CONF, $reslist, $resource;
		
		$tm = HTTP::_GP('tm', 0);
		$kursatm			= $CONF['resource_multiplier'] < 50 ? 0.2 : ($CONF['resource_multiplier'] < 500 ? 0.0135 : 0.00572);
		if($USER['atm']>=$tm*$kursatm)
		{
			$tradeAmount = $tm;
		}
		else
		{
			$tradeAmount = $USER['atm'] / $kursatm;
		}
		$USER[$resource['931']]		-= $tradeAmount * $kursatm;
		$USER[$resource['921']]		+= $tradeAmount;
		
		$this->printMessage($LNG['tr_exchange_done'], array("game.php?page=trader", 3));
	}
	
	function send()
	{
		global $USER, $PLANET, $LNG, $CONF, $reslist, $resource;
		
		if ($USER['darkmatter'] < $CONF['darkmatter_cost_trader']) {
			$this->redirectTo('game.php?page=trader');
		}
		
		$resourceID	= HTTP::_GP('resource', 0);
		
		if(!in_array($resourceID, $reslist['resstype'][4])) {
			$this->printMessage($LNG['invalid_action']);
		}

		$getTradeResources	= HTTP::_GP('trade', array());
		
		$tradeResources		= array_values(array_diff($reslist['resstype'][4], array($resourceID)));
		$tradeSum 			= 0;
		
		foreach($tradeResources as $tradeRessID) {
			
			if($resourceID == '921')
				{
					if(max(0, ceil((float) $getTradeResources[$tradeRessID])* self::$Charge[$resourceID][$tradeRessID]) <= $USER[$resource[$resourceID]]-$CONF['darkmatter_cost_trader'])
					{
						$tradeAmount = max(0, ceil((float) $getTradeResources[$tradeRessID]));
					}
					else
					{
						$tradeAmount = round(($USER[$resource[$resourceID]]-$CONF['darkmatter_cost_trader']) / self::$Charge[$resourceID][$tradeRessID]);
					}
				}
			else
				{
					if(max(0, ceil((float) $getTradeResources[$tradeRessID]) * self::$Charge[$resourceID][$tradeRessID]) <= $PLANET[$resource[$resourceID]])
					{
						$tradeAmount = max(0, ceil((float) $getTradeResources[$tradeRessID]));
					}
					else
					{
						$tradeAmount = round($PLANET[$resource[$resourceID]] / self::$Charge[$resourceID][$tradeRessID]);
					}
				}
			
			$tradeSum	   += $tradeAmount;
			if(empty($tradeAmount)) {
				continue;  
			}
			
			if($resourceID == '921')
				{$USER[$resource[$resourceID]]		-= ceil($tradeAmount * self::$Charge[$resourceID][$tradeRessID]);}
			else
				{$PLANET[$resource[$resourceID]]	-= ceil($tradeAmount * self::$Charge[$resourceID][$tradeRessID]);}
			if($tradeRessID != '921')		
				{$PLANET[$resource[$tradeRessID]]		+= $tradeAmount;}
		}
		
		if ($tradeSum > 0)
			$USER[$resource[921]]	-= $CONF['darkmatter_cost_trader'];

		$this->printMessage($LNG['tr_exchange_done'],true,  array("game.php?page=trader", 3));

	}
}
?>